﻿using System.Data.Entity.Migrations;
using System.Linq;
using System.Web.Mvc;
using Cnm.Models;
using Cnm.Models.Admins;
using Cnm.Mvc.Web.Framework.Controllers.Attributes;
using Cnm.Servicers.Localization;
using Cnm.Servicers.Seo;
using Kendo.Mvc.UI;

namespace Cnm.Mvc.Admin.Controllers
{
    public class MngProductController : BaseAdminController
    {
        private readonly ILanguageService _languageService;
        private readonly CnmDemoEntities dbContext = new CnmDemoEntities();
        private readonly ILocalizedEntityService _localizedEntityService;
        private readonly IUrlRecordService _urlRecordService;


        public MngProductController(
            ILanguageService languageService,
            ILocalizedEntityService localizedEntityService,
            IUrlRecordService urlRecordService)
        {
            this._languageService = languageService;
            this._localizedEntityService = localizedEntityService;
            this._urlRecordService = urlRecordService;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetProductsViewModel([DataSourceRequest] DataSourceRequest request)
        {
            var result = dbContext.Products;
            return ReturnJsonForGrid(result, result.Count(), request);
        }

        public ActionResult Create()
        {
            var model = new ProductModel();
            AddLocales(_languageService, model.Locales);

            return View(model);
        }

        [HttpPost, ParameterBasedOnFormName("save-continue", "continueEditing")]
        public ActionResult Create(ProductModel model, bool continueEditing)
        {
            if (ModelState.IsValid)
            {
                //product
                var product = model.ToEntity();
                dbContext.Products.Add(product);
                dbContext.SaveChanges();
                
                _localizedEntityService.SaveLocals(product, model.Locales);
                _urlRecordService.ValidateAndSaveSlug(product, model.SeName, model.Name, 0);
                
                foreach (var localized in model.Locales)
                {
                    _urlRecordService.ValidateAndSaveSlug(product, localized.SeName, localized.Name, localized.LanguageId);
                }
                
                if (continueEditing)
                    return RedirectToAction("Edit", new {id = product.Id});
                return RedirectToAction("Index");
            }
            return View(model);
        }

        public ActionResult Edit(int id)
        {
            var product = dbContext.Products.FirstOrDefault(p => p.Id == id);
            var model = product.ToModel();
            model.SeName = product.GetSeName(0);
            AddLocales(_languageService, model.Locales, (locale, languageId) =>
            {
                locale.Name = product.GetLocalized(x => x.Name, languageId, false, false);
                locale.ShortDescription = product.GetLocalized(x => x.ShortDescription, languageId, false, false);
                locale.FullDescription = product.GetLocalized(x => x.FullDescription, languageId, false, false);
                locale.SeName = product.GetSeName(languageId, false, false);
            });


            return View(model);
        }

        [HttpPost, ParameterBasedOnFormName("save-continue", "continueEditing")]
        public ActionResult Edit(ProductModel model, bool continueEditing)
        {
            if (ModelState.IsValid)
            {
                var product = model.ToEntity();
                dbContext.Products.AddOrUpdate(product);
                dbContext.SaveChanges();

                //locales
                _localizedEntityService.SaveLocals(product, model.Locales);
                //search engine name
                _urlRecordService.ValidateAndSaveSlug(product, model.SeName, model.Name, 0);

                foreach (var localized in model.Locales)
                {
                    _urlRecordService.ValidateAndSaveSlug(product, localized.SeName, localized.Name, localized.LanguageId);
                }

                if (continueEditing)
                    return RedirectToAction("Edit");
                return RedirectToAction("Index");
            }
            return View(model);
        }

    }
}